﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using WebMobie.Models;
using WebMobie.Models.Entities;
using System.Web.Http;
using System.Web.Mvc;


namespace WebMobie.Controllers
{
    public class NhapLieuController : Controller
    {
        private readonly WebMobileContext db = new WebMobileContext();


        [System.Web.Mvc.HttpGet]
        public ActionResult nhapLieuHoaDon()
        {
            for(int i=0;i<100;i++)
            {
                Order iOrder = getRandomizeHoaDon();
                addHoaDon(iOrder);
                Console.Write("added [%i]",i);
            }
            return View();
        }

        private void addHoaDon(Order iOrder)
        {
            db.Orders.Add(iOrder);
            for (int i = 0; i < iOrder.OrderDetails.Count; i++)
            {
                OrderDetail iDetail = iOrder.OrderDetails.ElementAt(i);
                db.OrderDetails.Add(iDetail);
            }
            db.SaveChanges();
        }

        private Order getRandomizeHoaDon()
        {
            Order tempOrder = new Order();

            tempOrder.Seller = db.getRandomUser();
            tempOrder.ShippingInfo = null;
            Random rd = new Random();
            int randomTimes = rd.Next(1,10);
            ICollection<OrderDetail> list = new List<OrderDetail>();
            for(int i=0;i<randomTimes;i++)
            {
                
                OrderDetail iDetail = getRandomOrderDetail();
                iDetail.Order = tempOrder;
                list.Add(iDetail);

            }
            
            tempOrder.OrderDetails = list;
            tempOrder.autoGetTotal();
            tempOrder.Status = 3;
            tempOrder.CustomerName = "hehe";
            tempOrder.CustomerPhone = "2481952";
            int randomMonth = rd.Next(12)+1;
            int randomDay = rd.Next(28)+1;
            tempOrder.DateCreated = new DateTime(DateTime.Now.Year, randomMonth, randomDay);

            return tempOrder;
            
            
        }
        private OrderDetail getRandomOrderDetail()
        {
            OrderDetail orderDetail = new OrderDetail();
            Random rd = new Random();

            orderDetail.Quantity = rd.Next(1, 5);
            
            
            orderDetail.Product = db.getRandomProduct();
            orderDetail.UnitCost = orderDetail.Product.ActualCost;
            orderDetail.ProductId = orderDetail.Product.ProductId;
            orderDetail.autoCountTotal();
            return orderDetail;
        }
    }
}
